//
//  LoginViewController.m
//  BaJieLvYouForIphone
//
//  Created by zhangwo on 16/8/12.
//  Copyright © 2016年 GuoBuGa. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "FindPassViewController.h"
#import "DSXNavigationController.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

@synthesize usernameView = _usernameView;
@synthesize passwordView = _passwordView;
@synthesize loginButton  = _loginButton;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImageView *bgImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, SHEIGHT)];
    bgImage.image = [UIImage imageNamed:@"icon-bgIogin.png"];
    bgImage.userInteractionEnabled = YES;
    [self.view addSubview:bgImage];
    
    UIImageView *backImage = [[UIImageView alloc] initWithFrame:CGRectMake(20, 30, 20, 20)];
    backImage.image = [UIImage imageNamed:@"icon-return-back.png"];
    backImage.userInteractionEnabled=YES;
    [bgImage addSubview:backImage];
    UITapGestureRecognizer *singleTap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeLogin)];
    [backImage addGestureRecognizer:singleTap];
    
    UIButton *registerBtn = [[UIButton alloc] initWithFrame:CGRectMake(SWIDTH-80, 30, 80, 25)];
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [registerBtn addTarget:self action:@selector(showRegister) forControlEvents:UIControlEventTouchUpInside];
    [bgImage addSubview:registerBtn];
    
    UIImageView *loginView = [[UIImageView alloc] initWithFrame:CGRectMake(10, SWIDTH/2, SWIDTH-24, 121)];
    loginView.image = [UIImage imageNamed:@"icon-login-image.png"];
    loginView.userInteractionEnabled = YES;
    loginView.layer.cornerRadius = 10;
    
    _line = [[UIView alloc] initWithFrame:CGRectMake(0, 60, loginView.frame.size.width, 1)];
    _line.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.2];
    [loginView addSubview:_line];
    
    [self.view addSubview:loginView];
    
    //用户名输入框
    _usernameView = [[LoginInputView alloc] initWithFrame:CGRectMake(0, 0, loginView.frame.size.width, 60)];
    _usernameView.placeHolder = @"手机号";
    _usernameView.textField.delegate = self;
    [loginView addSubview:_usernameView];
    
    //密码输入框
    _passwordView = [[LoginInputView alloc] initWithFrame:CGRectMake(0, 61, loginView.frame.size.width, 60)];
    _passwordView.placeHolder = @"输入登录密码,至少6位";
    _passwordView.textField.secureTextEntry = YES;
    [loginView addSubview:_passwordView];
    
    _loginButton = [DSXUI longButtonWithTitle:@"登录"];
    _loginButton.frame = CGRectMake(25, SWIDTH, SWIDTH-50, 50);
    _loginButton.layer.cornerRadius = 10;
    [_loginButton setBackgroundImage:[UIImage imageNamed:@"icon-login-regist-image.png"] forState:UIControlStateNormal];
    [_loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_loginButton];
    
    NSDictionary *loginInfo = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"loginInfo"];
    if (loginInfo) {
        _usernameView.text = [loginInfo objectForKey:@"account"];
        _passwordView.text = [loginInfo objectForKey:@"password"];
    }
    
    //找回密码
    UIButton *findPAssword = [[UIButton alloc] initWithFrame:CGRectMake(20, SWIDTH/2+120, 120, 30)];
    [findPAssword setTitle:@"忘记密码?" forState:UIControlStateNormal];
    [findPAssword setTitleColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.8] forState:UIControlStateNormal];
    [findPAssword.titleLabel setFont:[UIFont systemFontOfSize:16.0]];
    [findPAssword addTarget:self action:@selector(findPassword:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:findPAssword];
}

//- (void)viewWillDisappear:(BOOL)animated
//{
//    [super viewDidAppear:animated];
//    self.navigationController.navigationBarHidden = NO;
//}
//
//- (void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//    self.navigationController.navigationBarHidden = YES;
//}

- (void)closeLogin{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)showRegister{
    RegisterViewController *registerController = [[RegisterViewController alloc] init];
    [self presentViewController:registerController animated:YES completion:nil];
}

- (void)findPassword:(UIButton *)button{
    FindPassViewController *findView = [[FindPassViewController alloc] init];
    findView.step = 1;
    [self.navigationController pushViewController:findView animated:YES];
}

- (void)login{
    
    NSString *account  = _usernameView.text;
    NSString *password = _passwordView.text;
    if ([account length] < 1) {
        [[DSXUI standardUI] showPopViewWithStyle:DSXPopViewStyleDefault Message:@"账号输入错误"];
        return;
    }
    
    if ([password length] < 6) {
        [[DSXUI standardUI] showPopViewWithStyle:DSXPopViewStyleDefault Message:@"密码不能少于6位"];
        return;
    }
    
    NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
    [userInfo setObject:account forKey:@"account"];
    [userInfo setObject:password forKey:@"password"];
    
    [[DSXActivityIndicator sharedIndicator] showModalViewWithTitle:@"登录中,请稍后..."];
        [[BJUserStatus sharedStatus] login:userInfo success:^(id responseObject) {
            [[NSUserDefaults standardUserDefaults] setObject:userInfo forKey:@"loginInfo"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [[DSXActivityIndicator sharedIndicator] hide];
                [self dismissViewControllerAnimated:YES completion:nil];
            });
        } failure:^(NSString *errorMsg) {
            [[DSXActivityIndicator sharedIndicator] hide];
            [[DSXUI standardUI] showPopViewWithStyle:DSXPopViewStyleDefault Message:errorMsg];
        }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

#pragma mark -
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return true;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
